package com.haibei.activity.setting;

import java.util.HashMap;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.common.net.NetResult;
import com.common.task.BaseTask;
import com.common.task.NetCallBack;
import com.common.uitl.StringUtils;
import com.common.uitl.Tool;
import com.haibei.App;
import com.haibei.R;
import com.haibei.activity.BaseHaibeiActivity;
import com.haibei.net.NetInterface;

public class ChangePasswordActivity extends BaseHaibeiActivity implements OnClickListener {

	private BaseTask mBaseTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	@Override
	public void initView() {
		setContentView(R.layout.activity_setting_change_pwd);
		findButtonById(R.id.buttonBack).setOnClickListener(this);
		findButtonById(R.id.buttonCompte).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.buttonBack:
			finish();
			break;
		case R.id.buttonCompte:
			if (checkValue()) {
				changePasswd(getInputFromId(R.id.editTextNewPasswordRepeat));
			}
			break;
		}
	}

	private boolean checkValue() {
		String oldPasswd = getInputFromId(R.id.editTextOrginPassword);
		if (!App.getApp().getUser().getPassword().equals(oldPasswd)) {
			Tool.ToastShow(ChangePasswordActivity.this, "原密码错误！");
			return false;
		}
		String newPassword = getInputFromId(R.id.editTextNewPassword);
		if (StringUtils.isEmpty(newPassword) || newPassword.length() < 6) {
			Tool.ToastShow(ChangePasswordActivity.this, "请输入至少6位新密码！");
			return false;
		}
		String newPasswordRepeat = getInputFromId(R.id.editTextNewPasswordRepeat);
		if (StringUtils.isEmpty(newPasswordRepeat) || !newPasswordRepeat.equals(newPassword)) {
			Tool.ToastShow(ChangePasswordActivity.this, "请输入正确的确认密码！");
			return false;
		}
		return true;
	}

	private void changePasswd(String newPasswd) {
		// username, oldPassword, newPassword
		HashMap<String, String> hmap = new HashMap<String, String>();
		hmap.put("username", App.getApp().getUser().getUsername());
		hmap.put("oldPassword", App.getApp().getUser().getPassword());
		hmap.put("newPassword", newPasswd);
		getBaseTask().execute(hmap);
	}

	private BaseTask getBaseTask() {
		if (null != mBaseTask && mBaseTask.isCancelled()) {
			mBaseTask.cancel(true);
		}
		mBaseTask = new BaseTask(new NetCallBack() {
			@Override
			public void onPreCall() {
				showProgressDialogWithTask(mBaseTask);
			}

			@Override
			public void onFinish(NetResult result) {
				hideProgressDialogWithTask();
				if (null != result) {
					if (result.isSuccess()) {
						Tool.ToastShow(ChangePasswordActivity.this, "密码修改成功！");
						finish();// 自动回退
					} else {
						Tool.ToastShow(ChangePasswordActivity.this, result.getMessage());
					}
				} else {
					Tool.ToastShow(ChangePasswordActivity.this, R.string.error_net);
				}
			}

			@Override
			public NetResult onDoInBack(HashMap<String, String> paramMap) {
				NetResult result = null;
				try {
					result = NetInterface.modefyPasswd(ChangePasswordActivity.this, paramMap);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}
		});
		return mBaseTask;
	}
}
